﻿Public Class AddFundInvLink
#Region "Declare"
    Dim _dtLinked As DataTable = Nothing
    Private _InvestorID As Integer

#End Region

#Region "Properties"
    Public Property dtLinked() As DataTable
        Get
            Return _dtLinked
        End Get
        Set(ByVal value As DataTable)
            _dtLinked = value
        End Set
    End Property

    Public Property InvestorID() As Integer

        Get
            Return _InvestorID
        End Get

        Set(ByVal value As Integer)
            _InvestorID = value
        End Set

    End Property
#End Region

#Region "Method"

    Public Sub UpdateCheckColumnStatus()
        If dtLinked IsNot Nothing Then
            For Each row As DataRow In dtLinked.Rows
                If CType(grdInvestors.DataSource, DataTable).Columns.Contains("FundID") Then
                    Dim FilterQuery As String
                    Dim Excel() As DataRow
                    FilterQuery = "FundID ='" + Trim(row("FundID")).Replace("'", "''") + "'"
                    Excel = CType(grdInvestors.DataSource, DataTable).Select(FilterQuery)
                    If Excel.Length > 0 Then
                        Excel(0)("Link") = True
                        'Excel(0)("Link").Activation = Activation.Disabled
                    End If
                End If
            Next
        End If
        For Each row As UltraGridRow In grdInvestors.Rows
            If row.Cells("Link").Value Then
                row.Cells("Link").Activation = Activation.Disabled
            End If
        Next
    End Sub

    Public Sub GetInvestorByFundID(ByVal fundName As String)
        Dim idr As IDataReader = Nothing
        Try
            Dim dtInvestor As New DataTable
            idr = cAddModifyInvestor.GetFundsByInvestors(fundName)
            dtInvestor.Load(idr)
            ChangeDataReaderDataTable(dtInvestor)
            grdInvestors.DataSource = dtInvestor
            'grdInvestors.DataBind()
            With grdInvestors
                If .DisplayLayout.Bands(0).Columns.Exists("FundID") Then
                    .DisplayLayout.Bands(0).Columns("FundID").Hidden = True
                End If

            End With
        Catch ex As Exception

        Finally
        End Try

        'idr = Nothing
    End Sub

    Private Function CreateDT()
        Dim clone As DataTable = Nothing
        Try
            clone = New DataTable
            clone.Columns.Add("FundID", GetType(Integer))
            clone.Columns.Add("FundName", GetType(String))

            Return clone
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
            Return Nothing
        Finally
            clone = Nothing
        End Try
    End Function

#End Region

#Region "Events"
    Private Sub txtFundName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFundName.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtFundName.Text.Trim().Length = 0 Then
                Return
            Else
                GetInvestorByFundID(txtFundName.Text)
                UpdateCheckColumnStatus()
                grdInvestors.DisplayLayout.Bands(0).Columns("FundName").CellActivation = Activation.ActivateOnly

            End If
        End If
    End Sub



    Private Sub btnSaveInv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveInv.Click
        Dim AddInvestorTable As DataTable = CreateDT()
        Dim IdString As String = String.Empty
        For Each row As UltraGridRow In grdInvestors.Rows
            If row.Cells("Link").Value = True And Not row.Cells("Link").Activation = Activation.Disabled Then
                If dtLinked.Columns.Contains("FundID") Then
                    Dim FilterQuery As String
                    Dim Excel() As DataRow
                    Dim checkedRow As DataRow
                    Dim Id As Integer
                    Dim Name As String

                    FilterQuery = "FundID ='" + Trim(row.Cells("FundID").Value).Replace("'", "''") + "'"
                    Excel = dtLinked.Select(FilterQuery)
                    If Excel.Length = 0 Then
                        checkedRow = AddInvestorTable.NewRow()
                        Id = Convert.ToInt32(row.Cells("FundID").Text)
                        Name = row.Cells("FundName").Value
                        checkedRow("FundID") = Id
                        checkedRow("FundName") = Name
                        AddInvestorTable.Rows.Add(checkedRow)
                    End If
                End If
            End If

        Next

        dtLinked = AddInvestorTable
        Me.Close()
    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        dtLinked = Nothing
        Me.Close()
    End Sub

    Private Sub grdInvestors_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdInvestors.MouseClick
        If grdInvestors.ActiveCell IsNot Nothing Then
            If (grdInvestors.ActiveCell.Column.Header.Caption = "Link") Then
                If (grdInvestors.ActiveCell.Value = True) Then
                    grdInvestors.ActiveRow.Cells("Link").Value = False
                Else
                    grdInvestors.ActiveRow.Cells("Link").Value = True
                End If
                CType(grdInvestors.DataSource, DataTable).AcceptChanges()
            End If
        End If
    End Sub

    Private Sub AddFundInvLink_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.grdInvestors.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
    End Sub
#End Region
End Class